//
//  deviceView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/5/21.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "deviceView.h"

@interface deviceView () <UITableViewDelegate, UITableViewDataSource>
{
    NSArray *devices, *others;
    NSInteger nowValue;
}

@property (nonatomic, strong) UITableView *deviceTableView;

@end

@implementation deviceView

#pragma mark - init
- (id)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self addBackView];
        nowValue = 0;
        [self addSubview:self.deviceTableView];
    }
    return self;
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [devices count] + [others count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndetify = @"deviceCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetify];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetify];
    }
    for (UIView *view in cell.subviews) {
        if([view isKindOfClass:[UIImageView class]] || [view isKindOfClass:[UIButton class]] || [view isKindOfClass:[UISlider class]]){
            [view removeFromSuperview];
        }
    }
    UIImageView *imageView;
    if(indexPath.row < [devices count]){
        cell.textLabel.text = [NSString stringWithString:[[devices objectAtIndex:indexPath.row] objectForKey:@"deviceName"]];
        NSString *deviceType = [[devices objectAtIndex:indexPath.row] objectForKey:@"deviceType"];
        UIImage *image;
        if([deviceType isEqualToString:AIR_TYPE]){
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_airconditioning");
        }else if([deviceType isEqualToString:TV_TYPE]){
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_tv");
        }else if([deviceType isEqualToString:LIGHT_TYPE1] || [deviceType isEqualToString:LIGHT_TYPE2] || [deviceType isEqualToString:LIGHT_TYPE3]){
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_light");
            
        }else if([deviceType isEqualToString:CURTAIN_TYPE] || [deviceType isEqualToString:MB_TYPE]){
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_curtains");
        }
        else if([deviceType isEqualToString:STB_TYPE]){
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_stb");
        }else if([deviceType isEqualToString:DVD_TYPE]){
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_dvd");
        }else if([deviceType isEqualToString:AMP_TYPE]){
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_amp");
        }else{
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_lang");
        }
        imageView = [[UIImageView alloc] initWithImage:image];
        if([deviceType isEqualToString:LIGHT_TYPE3]){     //调光灯
            UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(self.bounds.size.width - 140, 10, 100, 30)];
            slider.tag  = indexPath.row;
            slider.maximumValue = 4.0;
            slider.minimumValue = 0.0;
            slider.value = nowValue;
            slider.minimumTrackTintColor = TEXT_GREEN_COLOR;
            [slider addTarget:self action:@selector(touchSlider:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:slider];
        }else{
            UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width - 120, 10, 30, 30)];
            UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width - 70, 10, 30, 30)];
            leftButton.tag = indexPath.row;
            rightButton.tag = indexPath.row;
            [leftButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnoff") forState:UIControlStateNormal];
            [rightButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnon") forState:UIControlStateNormal];
            [leftButton addTarget:self action:@selector(pressOffButton:) forControlEvents:UIControlEventTouchUpInside];
            [rightButton addTarget:self action:@selector(pressOnButton:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:leftButton];
            [cell addSubview:rightButton];
        }
        
    }else{
        imageView = [[UIImageView alloc] initWithImage:GET_IAMGE_WITH_NAME(@"Image/tableView/list_lang")];
        cell.textLabel.text = @"其他";
    }
    [imageView setFrame:CGRectMake(self.bounds.size.width * 0.4, 10, 30, 30)];
    [cell addSubview:imageView];
    
    UIImageView *rightImgaeView = [[UIImageView alloc] initWithImage:GET_IAMGE_WITH_NAME(@"Image/tableView/monitor_go")];
    [rightImgaeView setFrame:CGRectMake(self.bounds.size.width - 35, 10, 30, 30)];
    [cell addSubview:rightImgaeView];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.showFundationView(indexPath.row);
}

#pragma mark - response
- (void)pressOnButton:(UIButton *)button
{
    NSString *deviceType = [[devices objectAtIndex:button.tag] objectForKey:@"deviceType"];
    NSString *fundationName;
    if([deviceType isEqualToString:AIR_TYPE]){
        fundationName = AIR_ON;
    }else if([deviceType isEqualToString:TV_TYPE]){
        fundationName = TV_ON;
    }else if([deviceType isEqualToString:LIGHT_TYPE2]){
        fundationName = PD_ON;
    }else if([deviceType isEqualToString:CURTAIN_TYPE]){
        fundationName = CL_ON;
    }else if([deviceType isEqualToString:MB_TYPE]){
        fundationName = MB_ON;
    }else if([deviceType isEqualToString:STB_TYPE]){
        fundationName = STB_ON;
    }else if([deviceType isEqualToString:DVD_TYPE]){
        fundationName = DVD_ON;
    }else if([deviceType isEqualToString:AMP_TYPE]){
        fundationName = AMP_ON;
    }
    if(fundationName){
        self.pressResponse(fundationName, button.tag);
    }
}

- (void)pressOffButton:(UIButton *)button
{
    NSString *deviceType = [[devices objectAtIndex:button.tag] objectForKey:@"deviceType"];
    NSString *fundationName;
    if([deviceType isEqualToString:AIR_TYPE]){
        fundationName = AIR_OFF;
    }else if([deviceType isEqualToString:TV_TYPE]){
        fundationName = TV_OFF;
    }else if([deviceType isEqualToString:LIGHT_TYPE2]){
        fundationName = PD_OFF;
    }else if([deviceType isEqualToString:CURTAIN_TYPE]){
        fundationName = CL_OFF;
    }else if([deviceType isEqualToString:MB_TYPE]){
        fundationName = MB_OFF;
    }else if([deviceType isEqualToString:STB_TYPE]){
        fundationName = STB_OFF;
    }else if([deviceType isEqualToString:DVD_TYPE]){
        fundationName = DVD_OFF;
    }else if([deviceType isEqualToString:AMP_TYPE]){
        fundationName = AMP_OFF;
    }
    if(fundationName){
        self.pressResponse(fundationName, button.tag);
    }
}

- (void)touchSlider:(UISlider *)slider
{
    if(slider.value >= 0 && slider.value < 0.5){
        slider.value = 0;
    }else if(slider.value >= 0.5 && slider.value < 1.5){
        slider.value = 1;
    }else if(slider.value >= 1.5 && slider.value < 2.5){
        slider.value = 2;
    }else if(slider.value >= 2.5 && slider.value < 3.5){
        slider.value = 3;
    }else if(slider.value >= 3.5 && slider.value < 4.5){
        slider.value = 4;
    }
    if(slider.value == 0){
        self.pressResponse(TD_VALUE0, slider.tag);
    }else if(slider.value == 1){
        self.pressResponse(TD_VALUE1, slider.tag);
    }else if(slider.value == 2){
        self.pressResponse(TD_VALUE2, slider.tag);
    }else if(slider.value == 3){
        self.pressResponse(TD_VALUE3, slider.tag);
    }else if(slider.value == 4){
        self.pressResponse(TD_VALUE4, slider.tag);
    }
    nowValue = slider.value;
}

#pragma mark - method
- (void)showWithDeviceArray:(NSArray *)deviceArray OtherArray:(NSArray *)otherArray
{
    devices = deviceArray;
    others = otherArray;
    [self.deviceTableView reloadData];
}

- (void)addBackView
{
    UIImage *image = GET_IAMGE_WITH_NAME(@"Image/back_home_clear");
    UIImageView *backView = [[UIImageView alloc] initWithImage:image];
    CGFloat h = (self.bounds.size.height + 70) / 3;
    [backView setFrame:CGRectMake(0, 0, self.bounds.size.width, h)];
    [self addSubview:backView];
}

#pragma mark - getter
- (UITableView *)deviceTableView
{
    if(!_deviceTableView){
        CGFloat h = (self.bounds.size.height + 70) / 3;
        _deviceTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, h, self.bounds.size.width, self.bounds.size.height - h) style:UITableViewStylePlain];
        _deviceTableView.dataSource = self;
        _deviceTableView.delegate  =self;
        _deviceTableView.tableFooterView = [[UIView alloc] init];
        _deviceTableView.backgroundColor = BACKGROUND_COLOR;
    }
    return _deviceTableView;
}

@end
